package com.dshore.messagecenter.dao.system;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dshore.messagecenter.model.ExcelUp;
import com.dshore.messagecenter.model.system.User;
@Repository
public class LoginDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	public User findUserByUsername(String username) {
		String sql = "select * from T_SYS_USER where username=?";
		RowMapper<User> userRowMapper = new BeanPropertyRowMapper<>(User.class);
		return jdbcTemplate.queryForObject(sql, userRowMapper, username);
	}
	
	public Integer changePassword(String username,String password) {
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE T_SYS_USER SET PASSWORD =? WHERE USERNAME=?");
		int result=jdbcTemplate.update(sql.toString(),password,username);
		return result;
	}
	
	public Integer insertExcel(List<ExcelUp> list) {
		String sql="insert into T_EXCEL  values (?, ?, ?)";
		int i=0;
		for (ExcelUp excelUp : list) {
			int j=jdbcTemplate.update(sql,excelUp.getId(), excelUp.getName1(),excelUp.getName2());
			i=i+j;
		}
		return i;
	}
	
	public List<ExcelUp> xzexcel(){
		String sql="select * from T_EXCEL";
		RowMapper<ExcelUp> columnRowMapper = new BeanPropertyRowMapper<>(ExcelUp.class);
		return  jdbcTemplate.query(sql, columnRowMapper);
	}
	
	public Integer addIp(Map<String,Object> map) {
		String sql="insert into T_IP(ID,IP,CITY,SYSTEM,BROWSER)values(?,?,?,?,?)";
		return jdbcTemplate.update(sql, (String)map.get("id"),(String)map.get("ip"),(String)map.get("city"),(String)map.get("system"),(String)map.get("browser"));
	}
}
